package org.jerrymouse.jsa4j.vfs.local;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.jerrymouse.jsa4j.vfs.FileObject;

public class LocalFile extends LocalFileObject {

	public LocalFile(String basePath) {
		super(basePath);
	}

	public LocalFile(FileObject basePath, String name) {
		super(basePath, name);
	}

	public byte[] getContent() {
		InputStream input = null;
		byte[] bs = null;
		try {
			input = new FileInputStream(file);
			bs = IOUtils.toByteArray(input);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(input);
		}
		return bs;
	}

	public LocalFile write(byte[] content) {
		return write(content, false);
	}

	public LocalFile write(byte[] content, boolean append) {
		OutputStream outputStream = null;
		try {
			outputStream = new FileOutputStream(file, append);
			IOUtils.write(content, outputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outputStream);
		}
		return this;
	}

}